package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.DemainCommentBean;
import com.formiaomiao.demain.util.DateUtils;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.UseGlideUtils;

import java.util.List;

public class DemainCommentAdapter extends RecyclerView.Adapter<DemainCommentAdapter.ViewHolder> {

    Context context;
    List<DemainCommentBean.DataBean.ListBean> list;
    int demainuserid;

    private setViewClickListener viewClickListener;

    public interface setViewClickListener {
        void deletelistener(View v, int i);

        void checkdiscusslistener(View v, int i);

        void publishlistener(View v, int i);

        void headItemlistener(View v, int i);
    }

    public void setOnViewClickListener(setViewClickListener viewClickListener) {
        this.viewClickListener = viewClickListener;
    }

    public DemainCommentAdapter(Context context, List<DemainCommentBean.DataBean.ListBean> list, int demainuserid) {
        this.context = context;
        this.list = list;
        this.demainuserid = demainuserid;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.comment_item, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        UseGlideUtils.setGlide(context, list.get(i).getUserBean().getUserHead(), viewHolder.iv_userhead);
        viewHolder.tv_username.setText(list.get(i).getUserBean().getNickName());
        viewHolder.tv_time.setText(DateUtils.timess(Long.parseLong(list.get(i).getCreateDate())));
        viewHolder.tv_data.setText(list.get(i).getContent());
        if (list.get(i).getFromuid() == StaticValues.userid || demainuserid == StaticValues.userid) {
            viewHolder.tv_delete.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_delete.setVisibility(View.GONE);
        }
        if (list.get(i).getDiscusscount() == 0) {
            viewHolder.tv_publish.setVisibility(View.VISIBLE);
            viewHolder.tv_comment.setVisibility(View.GONE);
        } else {
            viewHolder.tv_publish.setVisibility(View.GONE);
            viewHolder.tv_comment.setVisibility(View.VISIBLE);
            viewHolder.tv_comment.setText("共有" + list.get(i).getDiscusscount() + "条回复");
        }
        viewHolder.tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.deletelistener(view, i);
            }
        });
        viewHolder.tv_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.publishlistener(view, i);
            }
        });
        viewHolder.tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.checkdiscusslistener(view, i);
            }
        });
        viewHolder.iv_userhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.headItemlistener(view, i);
            }
        });
        viewHolder.tv_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewClickListener.headItemlistener(view, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_username, tv_time, tv_delete, tv_comment, tv_data, tv_publish;
        ImageView iv_userhead;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_username = itemView.findViewById(R.id.tv_username);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_delete = itemView.findViewById(R.id.tv_delete);
            tv_comment = itemView.findViewById(R.id.tv_comment);
            tv_data = itemView.findViewById(R.id.tv_data);
            tv_publish = itemView.findViewById(R.id.tv_publish);
            iv_userhead = itemView.findViewById(R.id.iv_userhead);
        }
    }
}
